package stream;

import entity.Employee;
import org.junit.Test;
import org.omg.PortableInterceptor.INACTIVE;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class Stream {
    List<String> strings = Arrays.asList("afg", "sd", "t","", "sad", "12", "sdc","3","3", "3");

    @Test
    public void filter() {
        //过滤出长度大于1的字符
        List<String> collect = strings.stream().filter(s -> s.length() > 1).collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void distinct() {
        //去重复
        List<String> collect = strings.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);
        //对象是不能去除重复
        Employee employee = new Employee(1,"张三",34,345);
        Employee employee1 = new Employee(1,"张三",34,345);
        Employee employee2 = new Employee(1,"lisi",34,345);
        List<Employee> list = Arrays.asList(employee,employee1,employee2);
        System.out.println(list.stream().distinct().collect(Collectors.toList()));
    }

    @Test
    public void limit() {
        //从左往右保留两个元素
        List<String> collect = strings.stream().limit(2).collect(Collectors.toList());
        System.out.println(collect);
    }
    @Test
    public void skip() {
        //从左往右扔掉两个元素
        List<String> collect = strings.stream().skip(2).collect(Collectors.toList());
        System.out.println(collect);
    }
    @Test
    public void map() {
        //映射到每个元素操作,每个元素前面加个 *
        List<String> collect = strings.stream().map(s -> "*"+s).collect(Collectors.toList());
        System.out.println(collect);
    }

    @Test
    public void flatMap() {
//        List<String> collect = strings.stream().flatMap(s -> "*"+s).collect(Collectors.toList());
//        System.out.println(collect);
    }

    @Test
    public void allMatch(){
        //集合中的元素是否都满足
        List<Integer> list = Arrays.asList(23,45,64,34,234,65);
        //是否都大于10？
        boolean b = list.stream().allMatch(i -> i > 10);
        System.out.println(b);
    }
    @Test
    public void anyMatch(){
        //集合中的元素是否有一个满足
        List<Integer> list = Arrays.asList(23,45,64,34,234,65);
        //是否有一个大于200？
        boolean b = list.stream().anyMatch(i -> i > 200);
        System.out.println(b);
    }
    @Test
    public void noneMatch(){
        //集合中的元素都不满足条件
        List<Integer> list = Arrays.asList(23,45,64,34,234,65);
        //都不大于300？
        boolean b = list.stream().noneMatch(i -> i > 300);
        System.out.println(b);
    }

    @Test
    public void findAny(){
        //返回集合中的任何一个元素 并行流只能拿第一个
        List<Integer> list = Arrays.asList(23,45,64,34,234,65);
        Optional<Integer> any = list.stream().findAny();
        for (int i = 0; i < 1029763; i++) {
            System.out.println(any.get());
        }
    }

    @Test
    public void sorted(){
        //排序
        List<Integer> num = Arrays.asList(23,45,64,34,234,65);
        //默认排序 [23, 34, 45, 64, 65, 234] 从小到大
        List<Integer> numcollect = num.stream().sorted().collect(Collectors.toList());
        System.out.println(numcollect);

        //字母字典排序
        List<String> word = Arrays.asList("zhaf","hello","da","scg","fsdv","kff");
        List<String> wordcollect = word.stream().sorted().collect(Collectors.toList());
        System.out.println(wordcollect);

    }
}
